<?php
/**
 * Woosize class file.
 *
 * @author Fakis Chan <fakis738@gmail.com>
 * @link http://www.woosize.com/framework/
 * @license http://www.woosize.com/license/
 * @version $Id: Woosize.php 2012-12-08 Z Fakis Chan $
 * @package system
 * @since 1.0
 */

defined('WS_BEGIN_TIME') or define('WS_BEGIN_TIME', microtime(true));
defined('WS_DEBUG') or define('WS_DEBUG', false);
defined('WS_PATH') or define('WS_PATH', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);

class Woosize
{
    public static $classMap = array();
    public static $enableIncludePath = true;

    private static $_aliases = array('framework'=>WS_PATH);
    private static $_imports = array();
    private static $_includePaths;

    public static function getVersion()
    {
        return '1.0.0';
    }

    public static function getFrameworkPath()
    {
        return WS_PATH;
    }

    public static function import($alias, $forceInclude=false)
    {
        if(isset(self::$_imports[$alias]))
            return self::$_imports[$alias];

        if(class_exists($alias, false) || interface_exists($alias, false))
            return self::$_imports[$alias] = $alias;

        if(($pos=strrpos($alias, '.')) === false)
        {
            if($forceInclude && self::autoload($alias))
                self::$_imports[$alias] = $alias;
            return $alias;
        }

        $className = (string)substr($alias, $pos+1);
        $isClass = $className !== '*';

        if($isClass && (class_exists($className, false) || interface_exists($className, false)))
            return self::$_imports[$alias] = $className;

        if(($path = self::getPathOfAlias($alias)) !== false)
        {
            if($isClass)
            {
                if($forceInclude)
                {
                    if(is_file($path. '.php'))
                        require($path. '.php');
                    else
                        throw new CException();
                    self::$_imports[$alias] = $className;
                }
                else
                    self::$classMap[$className] = $path. '.php';
                return $className;
            }
            else
            {
                if(self::$_includePaths === null)
                {
                    self::$_includePaths = array_unique(explode(PATH_SEPARATOR, get_include_path()));
                    if(($pos = array_search('.', self::$_includePaths, true)) !== false)
                        unset(self::$_includePaths[$pos]);
                }

                array_unshift(self::$_includePaths, $path);

                if(self::$enableIncludePath && set_include_path('.'. PATH_SEPARATOR. implode(PATH_SEPARATOR, self::$_includePaths)) === false)
                    self::$enableIncludePath = false;

                return self::$_imports[$alias] = $path;
            }
        }
        else
            throw new CException();
    }

    /**
     * 自动加载 __autoload
     */
    public static function autoload($className)
    {
        if(isset(self::$classMap[$className]))
            include(self::$classMap[$className]);
        else if(isset(self::$_coreClasses[$className]))
            include(WS_PATH. self::$_coreClasses[$className]);
        else
        {
            if(self::$enableIncludePath === true)
            {
                foreach(self::$_includePaths as $path)
                {
                    $classFile = $path. DS. $className. '.php';
                    if(is_file($classFile))
                    {
                        include($classFile);
                        break;
                    }
                }
            }
            else
                include($className. '.php');
        }
        return true;
    }

    /**
     * 根据别名取得路径
     */
    public static function getPathOfAlias($alias)
    {
        if(isset(self::$_aliases[$alias]))
            return self::$_aliases[$alias];
        else if(($pos = strpos($alias, '.')) !== false)
        {
            $rootAlias = substr($alias, 0, $pos);
            if(isset(self::$_aliases[$rootAlias]))
                return self::$_aliases[$alias] = rtrim(self::$_aliases[$rootAlias]. DS. str_replace('.', DS, substr($alias, $pos+1)), '*'. DS);
        }
        return false;
    }

    /**
     * 给路径设置别名
     */
    public static function setPathOfAlias($alias, $path)
    {
        if(empty($path))
            unset(self::$_aliases[$alias]);
        else
            self::$_aliases[$alias] = rtrim($path, '\\/');
    }

    public static function registerAutoloader($callback, $append=false)
    {
        if($append)
        {
            self::$enableIncludePath = false;
            spl_autoload_register($callback);
        }
        else
        {
            spl_autoload_unregister(array('Woosize', 'autoload'));
            spl_autoload_register($callback);
            spl_autoload_register(array('Woosize', 'autoload'));
        }
    }

    private static $_coreClasses = array(
        'CComponent'    => '/base/CComponent.php',
        'CException'    => '/base/CException.php',
        'CModel'        => '/base/CModel.php',
        'CModule'       => '/base/CModule.php',
        'CSite'         => '/base/CSite.php',
        'CDbConnection' => '/db/CDbConnection.php',
    );
}

spl_autoload_register(array('Woosize', 'autoload'));
require(WS_PATH. '/base/interfaces.php');

class ws extends Woosize{}

